/**
 * 
 */
package com.hs.core.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

/**
 * @author shl
 *
 */
public class ExcelUtils {

	 /**  
	   * 读取单个sheet的Excel,  
	   * @param path  
	   * @return list的第一个值是map的key值  
	   */  
	  public static List<Map<String, String>> readExcel(String path) {   

	      File file = null;   
	      Workbook book = null;   
	      Sheet[] sheets = null;   
	      Sheet sheet = null;   
	      Map<String, String> map = null;   
	      List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();   
	         
	      Map<String, String> keyMap = new HashMap<String, String>();   
	         
	      try {   
	          file = new File(path);   
	          book = Workbook.getWorkbook(file);   
	          //获取到所有工作表   
	          sheets = book.getSheets();   
	          if (null != sheets && 0 != sheets.length) {   
	              sheet = sheets[0];   
	              System.out.println("工作表对象的名字：" + sheet.getName() + "\n"    
	                      + "工作表对象的行数：" + sheet.getRows() + "\n"    
	                      + "工作表对象的列数：" + sheet.getColumns());   
	                 
	              //第一行即所有map的键值   
	              for (int i = 0; i < sheet.getColumns(); i++) {   
	                  String mapKey = sheet.getCell(i, 0).getContents();   
	                  keyMap.put(String.valueOf(i), handleKey(mapKey));   
	              }   
	              mapList.add(keyMap);   
	                 
	              //行循环，一行就一个map   
	              for (int j = 1; j < sheet.getRows(); j++) {   
	                  map = new HashMap<String, String>();   
	                  //列循环   
	                  for (int k = 0; k < sheet.getColumns(); k++) {   
	                      //标题栏   
	                      String key = sheet.getCell(k, 0).getContents();   
	                      Cell value = sheet.getCell(k, j);   
	                      System.out.println("坐标为 (" + k + ", " + j + ") 的值为 == > " + value.getContents());   
	                      map.put(handleKey(key), value.getContents());   
	                  }   
	                  mapList.add(map);   
	              }   
	          }   
	          book.close();   
	      } catch (Exception e) {   
	          System.out.println("读取Excel失败！");   
	          e.printStackTrace();   
	      }   
	      return mapList;   
	  }   
	     
	  /**  
	   * 处理key，形如"XX(XX)"，变为"XX"  
	   * @param key  
	   * @return  
	   */  
	  public static String handleKey(String key) {   
	      String str = "";   
	      if (null != key && 0 != key.trim().length()) {   
	          int i = 0;   
	          //英文括号   
	          if (-1 != key.indexOf("(")) {   
	              i = key.indexOf("(");   
	              str = key.substring(0, i);   
	          //中文括号   
	          } else if (-1 != key.indexOf("（")) {   
	              i = key.indexOf("（");   
	              str = key.substring(0, i);   
	          } else {   
	              str = key;   
	          }   
	       }   
	       return str.trim().toString();   
	   }   
}
